package com.acmedcare.framework.exchange.center.endpoint;

import com.acmedcare.framework.exchange.center.endpoint.exception.InvalidRequestParameterException;
import com.acmedcare.framework.exchange.center.endpoint.util.CommonUtil;
import com.acmedcare.framework.exchange.center.endpoint.util.HDFSCommon;
import com.acmedcare.framework.exchange.center.entity.dto.Hdfs;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.File;
import org.apache.hadoop.conf.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Api(tags = "文件系统api")
@RequestMapping("/file-system")
public class FileSystemEndpoint {


  private final Hdfs hdfs;

  public FileSystemEndpoint(Hdfs hdfs) {
    this.hdfs = hdfs;
  }

  @ApiOperation("上传文件到HDFS当中")
  @PostMapping("/hdfs/upload")
  public ResponseEntity<?> uploadHdfs(@RequestParam("file") MultipartFile file)
      throws InvalidRequestParameterException {

    if (file != null) {

      File convertFile = CommonUtil.MultipartFileToFile(file);

      String uri = HDFSCommon.copyfileToHdfs(new Configuration(),hdfs, convertFile);
      return ResponseEntity.ok(uri);
    }

    throw new InvalidRequestParameterException("传入文件不允许为空");
  }
}
